﻿using HR.Config;
using HR.Filters;
using HR.IService;
using HR.Models;
using Microsoft.AspNetCore.Mvc;

namespace HR.Controllers
{
    public class RoleController : Controller
    {
        private readonly IUsersService ius;
        private readonly IRolesService irs;

        public RoleController(IUsersService ius, IRolesService irs)
        {
            this.ius = ius;
            this.irs = irs;
        }

        [TypeFilter(typeof(RoleFilterAttribute))]
        /// <summary>
        /// 用户管理视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UserIndex()
        {
            return View();
        }
        /// <summary>
        /// 分页查询所有用户信息，刷新
        /// </summary>
        /// <param name="pgsize"></param>
        /// <param name="pgcurrent"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> RoleFillUsers(int pgsize,int pgcurrent)
        {
            Paging<UserDapper> pg = await ius.RoleFillUsers(pgsize,pgcurrent);
            return Json(pg);
        }
        /// <summary>
        /// 查询所有角色的数据集用于下拉列表框
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RoleFillRoles()
        {
            List<Role> list = await irs.RoleFillRoles();
            return Json(list);
        }
        /// <summary>
        /// 新增用户的接口
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RoleInsertUsers([FromBody] user us)
        {
            int res = await ius.RoleInsertUsers(us);
            return Json(res);
        }
        /// <summary>
        /// 修改用户的接口
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RoleUpdateUsers([FromBody] user us)
        {
            int res = await ius.RoleUpdateUsers(us);
            return Json(res);
        }
        /// <summary>
        /// 删除用户的接口
        /// </summary>
        /// <param name="us"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> RoleDeleteUsers(short u_id)
        {
            int res = await ius.RoleDeleteUsers(u_id);
            return Json(res);
        }
        [TypeFilter(typeof(RoleFilterAttribute))]
        /// <summary>
        /// 角色管理视图
        /// </summary>
        /// <returns></returns>
        public IActionResult RoleIndex()
        {
            return View();
        }
        /// <summary>
        /// 查询所有角色的数据集带分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> RoleFillRolesPG(int pgsize,int pgcurrent)
        {
            Paging<Role> list = await irs.RoleFillRolesPG(pgsize,pgcurrent);
            return Json(list);
        }
        /// <summary>
        /// 新增角色的接口
        /// </summary>
        /// <param name="re"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RoleInsertRole([FromBody] Role re)
        {
            int res = await irs.RoleInsertRole(re);
            return Json(res);
        }
        /// <summary>
        /// 查询所有菜单，以树形菜单展示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> FillMenus()
        {
            List<MenuRole> list = await ius.HomeMenusFill(0);
            return Json(list);
        }
        /// <summary>
        /// 根据角色ID查询到隶属于该角色下的已有权限，做回显
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> FindMenusByRid(int rid)
        {
            List<int> list = await irs.FindMenusByRid(rid);
            return Json(list);
        }
        /// <summary>
        /// 新增或修改的角色权限中间表集合
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RoulMenuMidUdpOrAdd([FromBody] List<RoleMenuMid> list)
        {
            int res = await irs.RoulMenuMidUdpOrAdd(list);
            return Json(res);
        }
        /// <summary>
        /// 根据角色ID修改角色信息
        /// </summary>
        /// <param name="re"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> RoleUpdateRole([FromBody] Role re)
        {
            int res = await irs.RoleUpdateRole(re);
            return Json(res);
        }
        [HttpDelete]
        public async Task<IActionResult> RoleDeleteRole(int rid)
        {
            int res = await irs.RoleDeleteRole(rid);
            return Json(res);
        }
    }
}
